package com.heisenhub.uua.service.impl;

import com.heisenhub.common.oauth.entity.UuaUser;
import com.heisenhub.api.systemapi.UserService;
import com.heisenhub.uua.service.HubUserDetailsService;
import com.heisnenhub.common.module.entity.SystemUser;
import com.heisnenhub.common.module.exception.RRException;
import com.heisnenhub.common.module.utils.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @author sen
 * @description
 * @date 2024/3/19 14:51
 */
@Service
@Slf4j
public class UuaUserServiceImpl implements HubUserDetailsService {



    @Autowired
    private UserService userService;

    /**
     *  用户登入
     * @param username 用户名
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        log.info("【准备登入.....】 {}" ,username);
        Api<SystemUser> userApi = userService.LoginByName(username);
        return buildUuaUser(userApi);
    }



    @Override
    public UserDetails loadUserByPhone(String phone) {
        log.info("【准备手机登录.....】 {}" ,phone);
        Api<SystemUser> userApi = userService.LoginByMobile(phone);
        return buildUuaUser(userApi);
    }


    private UuaUser buildUuaUser( Api<SystemUser>  userApi) {

        if(userApi.isOk() && Objects.nonNull(userApi.getData()) ) {
            SystemUser systemUser = userApi.getData();
            boolean notLocked = false;
            if (StringUtils.equals(SystemUser.STATUS_VALID, systemUser.getStatus()))
                notLocked = true;
            UuaUser uuaUser = new UuaUser(systemUser.getUsername(), systemUser.getPassword(), true, true, true, notLocked,
                    AuthorityUtils.commaSeparatedStringToAuthorityList(systemUser.getPermissions()));

            return transSystemUserToAuthUser(uuaUser, systemUser);
        }
        throw new RRException("登入失败");
    }



    private UuaUser transSystemUserToAuthUser( UuaUser uuaUser, SystemUser systemUser) {
        uuaUser.setAvatar(systemUser.getAvatar());
        uuaUser.setDeptId(systemUser.getDeptId());
        uuaUser.setDeptName(systemUser.getDeptName());
        uuaUser.setEmail(systemUser.getEmail());
        uuaUser.setMobile(systemUser.getMobile());
        uuaUser.setRoleId(systemUser.getRoleId());
        uuaUser.setRoleName(systemUser.getRoleName());
        uuaUser.setSex(systemUser.getSex());
        uuaUser.setUserId(systemUser.getUserId());
        uuaUser.setLastLoginTime(systemUser.getLastLoginTime());
        uuaUser.setDescription(systemUser.getDescription());
        uuaUser.setStatus(systemUser.getStatus());
        return uuaUser;
    }

}
